package com.study.transaction.dispatch.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.UUID;

/**
 * @author: chenhs
 * @date: Created in 14:47 2019/12/17
 **/
@Service
public class DispatchService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void dispatch(String orderId) throws SQLException {
        String dispatchSeq = UUID.randomUUID().toString();
        String sql = "insert into table_dispatch (dispatch_seq, order_id, dispath_content) values (?, ?, ?)";

        int update = jdbcTemplate.update(sql, dispatchSeq, orderId, "派送此订单");
        if (update != 1) {
            throw new SQLException("调度数据插入失败，原因[数据库操作]");
        }

    }
}
